/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ModuloConfiguracion;

import Acceso.Security;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.*;
import java.io.PrintWriter;
import Modelo.DAL.Grupo_DAO;
import Modelo.Grupo;

/**
 *
 * @author josmary
 */
public class consultarGruposAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String[] ALLOWED = {"Administrador", "Consultar Grupos", "Gestionar Grupos"};

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        String nombre = (String) request.getParameter("nombre");
        String sede = (String) request.getParameter("sede");
        String tipo = (String) request.getParameter("tipo");

        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");
        String asc = request.getParameter("asc");

        Grupo g = new Grupo();
        g.setSede(sede);
        g.setIdGrupo(nombre);
        g.setTipoGrupo(tipo);

        response.setContentType("text/json");
        JSONObject data = Grupo_DAO.listaGrupos(g, limit, offset, order, asc);

        PrintWriter pw = response.getWriter();
        pw.write(data.toString());
        pw.flush();
        pw.close();

        return null;
    }
}
